<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>搜索结果</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script src="/js/xkube.js?v=1" charset="utf-8"></script>
    <script src="/js/lay-config.js?v=1.0.4" charset="utf-8"></script>

<style type="text/css">
  .layui-table-cell {
    height: auto;
    line-height: 22px !important;
    text-overflow: inherit;
    overflow:ellipsis;
    white-space: normal;
  }
  .layui-table-cell .layui-table-tool-panel li {
    word-break: break-word;
  }
</style>
</head>
<body>
<blockquote class="layui-elem-quote layui-text">
    注:需将该接口:/task/v1/UpdateIndex 部署成定时任务,该接口不宜暴漏公网。每天执行一次更新资源索引，才会有搜索结果
</blockquote>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">关键字</label>
                            <div class="layui-input-inline" style="width:300px;">
                                <input type="text" name="keyword" id="keyword" autocomplete="off" placeholder="输入搜索内容" lay-verify="required" lay-reqtext="不能为空" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <table class="layui-table" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="detail">详情</a>
            <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="delete">删除</a>
        </script>
    </div>
</div>

</body>
<script>

var keyword = getQueryString("keyword");
console.log(deployApiVersion);
document.getElementById('keyword').value = keyword;

    layui.use(['form', 'table','miniTab'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
            miniTab = layui.miniTab,
            miniTab.listen();
      if (keyword != "") {
        table.render({
            elem: '#currentTableId',
            url: '/search/v1/List?keyword='+keyword,
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            parseData: function(res) { //实现加载全部数据后再分页
              if(res.code != 0){
                return {
               	  "code": res.code,
              	  "msg": res.msg,                 
                }
              }
            	if(this.page.curr) {
            		result=res.data.slice(this.limit*(this.page.curr-1),this.limit*this.page.curr);
            	}else{
            	  result=res.data.slice(0,this.limit);
              }
              return {
              	"code": res.code,
              	"msg":'',
              	"count":res.count,
              	"data":result
              };
            },
            cols: [[
                //{type: "checkbox", width: 50},
                {field: 'resName',title: '名称', sort: true},
                {field: 'clusterId',title: '集群ID', sort: true},
                {field: 'nameSpace',title: '命名空间', sort: true},
                {field: 'resType', title: '资源类型', sort: true},
                {field: 'message', title: '内容', sort: true},
                {field: 'labels', title: '标签',hide:true ,sort: true,templet: '#TagTpl'},
                {field: 'createtime', title: '索引时间'},
                {title: '操作', minWidth: 280, toolbar: '#currentTableBar', align: "center"}
            ]],
            size:'lg',
            limits: [25, 50, 100],
            limit: 25,
            page: true
        });
      }
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            data.field.keyword = data.field.keyword.replace(/^\s*|\s*$/g,"");
            table.reload('currentTableId', {
                method:'POST',
                url: '/search/v1/List',
                contentType: 'application/json',
                where: data.field,
                done: function(res, curr, count){
                }
            }, 'data');
            return false;
        }); 

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'detail') {
                 miniTab.openNewTabByIframe({
                      href:'/page/xkube/'+data.resType+'Detail.html?clusterId='+data.clusterId+'&nameSpace='+data.nameSpace+'&'+data.resType+'Name='+data.resName,
                      title: data.resName+"详情["+data.resType+"]",
                  });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定删除'+data.jobName+'?', {icon: 3, title:'删除提示',yes: function(index){
                     var index2 = layer.load(0, {shade: false});
                     layer.msg('此处需运行1-2s左右');
                			 $.ajax({
                			    url: '/search/v1/Del?id='+data.id,
                			    type: "get",
                			    //data: data,
                			    //dataType: "json",
                			    success: function (resp) {
                            layer.close(index2);
                						 if(resp.code == 0){
                							 layer.msg('删除成功', {icon: 1});
                							 //window.location.reload();
                							 obj.del();
                						 }else{
                							 layer.msg(resp.msg,{icon:2});
                						 }
                				  }
                		   });		  	  
                },
            		  cancel: function(index, layero){ 
            			    layer.close(index);
                      layer.close(index2);
            			    console.log("不操作");
            		  } 
        		    });                
            }
        });
    });
</script>

</body>
</html>